package com.china08.yxyapi.api.manage.tec.teaching.scoreport.selectexam;

import java.text.ParseException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.entity.mg.scoreport.Exam;
import com.china08.yxyapi.entity.mg.scoreport.Exam.BookContent;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.scoreport.ExamRepository;
import com.china08.yxyapi.util.DateUtils;

/**
 * 考试信息查询（修改&展示使用）
 * 
 */
@CrossOrigin
@RestController
public class SelectExamForOneController {

    @Autowired
    private ExamRepository examRepository;

    @RequestMapping(value = "/api_yw/manage/tec/teaching/scoreport/selectexam/getone", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel process(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel model)
            throws ParseException {
        model.check();

        Exam exam = examRepository.findOne(model.id);
        RespModel respModel = new RespModel();

        respModel.id = exam.getId();
        respModel.examId = exam.getExamId();
        respModel.name = exam.getName();
        respModel.type = exam.getType();
        respModel.stage = exam.getStage();
        respModel.subject = exam.getSubject();
        respModel.subjectName = exam.getSubjectName();
        respModel.versionName = exam.getVersionName();
        respModel.semester = exam.getSemester();
        respModel.semesterName = exam.getSemesterName();
        respModel.version = exam.getVersion();
        respModel.bookContents = exam.getBookContents();
        respModel.totalScore = exam.getTotalScore();
        respModel.examDate = DateUtils.formatDate2(DateUtils.formatStrDate4(exam.getExamDate()));
        respModel.questions = null == exam.getQuestions() || 0 == exam.getQuestions().size() ? "该试卷不存在小题数据"
                : "共" + exam.getQuestions().size() + "道题";
        respModel.classId = exam.getClassId();
        respModel.className = exam.getClassName();
        return respModel;
    }

    /**
     * 请求参数
     * 
     */
    protected static class ReqModel extends YxyApiReqModel {
        // 考试信息id
        public String id;

        public void check() {
            Assert.notNull(StringUtils.defaultIfBlank(id, null), "考试编号不能为空");
        }
    }

    /**
     * 响应参数
     * 
     */
    public static class RespModel extends YxyApiRespModel {
        public String id;
        // 考试编号
        public String examId;
        // 考试名称
        public String name;
        // 考试科目
        public String type;
        // 学段
        public String stage;
        // 学科
        public String subject;
        // 学科名称
        public String subjectName;
        // 版本名称
        public String versionName;
        // 版本
        public String version;
        // 册别
        public String semester;
        // 册别名称
        public String semesterName;
        // 知识点
        public List<BookContent> bookContents;
        // 试卷总分
        public Integer totalScore;
        // 考试时间
        public String examDate;
        // 试题提示
        public String questions;
        // 班级id
        public String classId;
        // 班级名称
        public String className;
    }
}
